#include<vector>
#include<queue>
#include<unordered_set>
using namespace std;
class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>>graph(numCourses);
        vector<int>inDegree(numCourses);
        for (auto& pre : prerequisites) {
            graph[pre[1]].push_back(pre[0]);
            ++inDegree[pre[0]];
        }
        queue<int>queue_;
        unordered_set<int>visited;
        vector<int>res;
        bool flag = true;
        for (int i = 0; i < inDegree.size(); ++i) {
            if (inDegree[i] == 0) {
                queue_.push(i);
            }
        }
        while (!queue_.empty()) {
            int idx = queue_.front();
            res.push_back(idx);
            queue_.pop();
            for (int i = 0; i < graph[idx].size(); ++i) {
                --inDegree[graph[idx][i]];
                if (inDegree[graph[idx][i]] == 0) {
                    queue_.push(graph[idx][i]);
                }
            }
        }
        if (res.size() != numCourses)
            res.clear();
        return res;
    }
};